package com.dotstone.chipism.util;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.dotstone.chipism.bean.HttpURL;

public class ConvertUtil {
	
	public static ConvertUtil mConvertor = null;
	
	public static ConvertUtil getInstance() {
		if (mConvertor == null) {
			mConvertor = new ConvertUtil();// 单例对象实例
		}
		return mConvertor;
	}

	private ConvertUtil() {

	}

	private Object readResolve() {
		return getInstance();
	}
	
	public static String convertUDPCmd(String mac, String manufacturer,
			String productId) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,10000");
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 19);
			jo1.put("manufacturer", manufacturer);
			jo1.put("productId", productId);
			jo1.put("mac", mac);
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public String convertHeartCmd() {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20," + SocketManager.getInstance().user_id);
			jo.put("d", "10,10000");
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 20);
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public String convertBindIdCmd(String deviceId){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 21);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return sendCmd;
	}
	
	public static String convertSearchCmd(int data[]) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 100);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public static String convertGetLockCmd(int data[]) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 103);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	private static String convertPowerCmd(int data[]){
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 100);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public static String transLightCmd(int data[]){
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 100);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public String convertMessionCmd(int [] data,String deviceId){
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", 20000);
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", "1");
			jo.put("f", System.currentTimeMillis());
			jo1.put("cmd",100);
			if (data != null && data.length != 0) {
				JSONArray jsonArray = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					jsonArray.put(data[i]);
				}
				jo1.put("data", jsonArray);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo1.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}


	public String convertCurtainCmd(int [] data,String deviceId){
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", 20000);
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", "1");
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd",101);
			if (data != null && data.length != 0) {
				JSONArray jsonArray = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					jsonArray.put(data[i]);
				}
				jo1.put("data", jsonArray);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	
	public String convertIrCmd(String str){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 120);
			jo1.put("ir_src", 1);
			jo1.put("ir_freq", 38000);
			jo1.put("ir_data",str);
			jo.put("g",""+jo1+"");
			sendCmd = jo1.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertSceneCmd(String mid, String cmd) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20," + SocketManager.getInstance().user_id);
			jo.put("d", "10," + mid);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
//			jo1.put("cmd", 103);
//			jo1.put("data", cmd);
			jo.put("g", cmd);
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	public String convertUpdateCmd(String deviceId,String version){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20004");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 6);
			jo1.put("downloadUrl", HttpURL.HARD_DEVICE_UPDATE_URL);
			jo1.put("upgradeNow", 1);
			jo1.put("version",version);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertNormalUpdateCmd(String deviceId,String version,String url){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20004");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 6);
			jo1.put("downloadUrl", url);
			jo1.put("upgradeNow", 1);
			jo1.put("version",version);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertTempCmd(String deviceId){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 110);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertInfraredCmd(String str){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 120);
			jo1.put("ir_src", 1);
			jo1.put("ir_freq", 38000);
			jo1.put("ir_data",str);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertStudyCmd(){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 121);
			jo.put("g",""+jo1+"");
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertUploadCmd(String deviceId,String g){
		String sendCmd = "";
		JSONObject jo = new JSONObject();
		try {
			jo.put("a", "20002");
			jo.put("b", 0);
			jo.put("c", "10,"+deviceId);
			jo.put("d", "30,30000");
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo.put("g",g);
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String convertCheckStateCmd(String mid,int cmd){
		String sendCmd = "";
		JSONObject jo1 = new JSONObject();
		JSONObject jo = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+mid);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", cmd);
			jo.put("g", jo1);
			sendCmd = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendCmd;
	}
	
	public String transGroundCmd(int [] data,String deviceId ){
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20,"+SocketManager.getInstance().user_id);
			jo.put("d", "10,"+deviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			jo1.put("cmd", 100);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}


	public static String convertWirelessCmd(int data[]) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20007");
			jo.put("b", 0);
			jo.put("c", "10,3");
			jo.put("d", "30,10000");
			jo.put("e", 1);
			jo.put("f", 0);
			jo.put("h", "0.0.1");
			jo1.put("cmd", 104);
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g",""+jo1+"");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}
	
	
}
